package com.taptech.doufu.presenter.home;

import android.app.Activity;

import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.bean.TagModel;
import com.taptech.doufu.bean.TagModelGsonBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


public class TagGovernmentPresenter {
    private OnTagView onTagView;
    public TagGovernmentPresenter(OnTagView onTagView) {
        this.onTagView = onTagView;
    }

    public interface OnTagView{
        void onResult(TagModel model);

        void onError();
    }

    public void getGovernmentTag(Activity activity,int sexuality, String topic_id) {
        ApiClient.getInstance().setUseCache(true).getService().getGovernmentTags(sexuality,topic_id)
                .compose(RxJavaHelper.<TagModelGsonBean>observeOnMainThread(activity))
                .subscribe(new BaseSubscriber<TagModelGsonBean>(){
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        onTagView.onError();
                    }

                    @Override
                    public void onNext(TagModelGsonBean tagModelGsonBean) {
                        super.onNext(tagModelGsonBean);
                        if (tagModelGsonBean != null &&
                                tagModelGsonBean.getData() != null) {
                            onTagView.onResult(tagModelGsonBean.getData());
                        }else{
                            onTagView.onError();
                        }
                    }
                });
    }



    /**
     * 格式化上传标签的数据
     * @return
     */
    public static ArrayList<String> getAllTagList(List<String> tagsList,HashMap<Integer,String> govermentTags) {
        ArrayList<String> list = new ArrayList<>();
        if (govermentTags != null && govermentTags.size() > 0) {
            Set<Integer> set = govermentTags.keySet();
            if (set != null) {
                for (Integer key:set) {
                    list.add(govermentTags.get(key)+"|"+key);
                }
            }
        }
        if (tagsList != null) {
            for (String tag:tagsList) {
                list.add(tag + "|0");
            }
        }
        return list;
    }
}
